package com.gz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.dao.QuestionLabelMapper;
import com.gz.dao.QuestionMapper;
import com.gz.dto.PageData;
import com.gz.dto.QuestionDto;
import com.gz.entity.LabelInfo;
import com.gz.entity.Question;
import com.gz.entity.QuestionLabel;
import com.gz.service.QuestionService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (Question)表服务实现类
 *
 * @author makejava
 * @since 2024-02-19 16:28:20
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionDao;
    @Autowired
    private QuestionLabelMapper questionLabelDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Question queryById(Integer id) {
        Question qt = questionDao.queryById(id);
        List<LabelInfo> labels = qt.getLabels();
        List<String> lbnms = labels.stream().map(LabelInfo::getLabelName).distinct()
                .collect(Collectors.toList());
        qt.setQuestionLabels(lbnms);
        return qt;
    }

    /**
     * 分页查询
     *
     * @param dto 筛选条件
     * @return 查询结果
     */
    @Override
    public PageData queryByPage(QuestionDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<Question> list = questionDao.queryAll(dto);
        for (Question qt : list) {
            List<LabelInfo> labels = qt.getLabels();
            List<String> lbnms = labels.stream().map(LabelInfo::getLabelName).distinct()
                    .collect(Collectors.toList());
            qt.setQuestionLabels(lbnms);
        }
        PageInfo<Question> pageInfo = new PageInfo<>(list);
        return new PageData<>(pageInfo.getTotal(), list);
    }

    /**
     * 分页查询
     *
     * @param dto 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Question> queryAll(QuestionDto dto) {
        List<Question> list = questionDao.queryAll(dto);
        for (Question qt : list) {
            List<LabelInfo> labels = qt.getLabels();
            List<String> lbnms = labels.stream().map(LabelInfo::getLabelName).distinct()
                    .collect(Collectors.toList());
            qt.setQuestionLabels(lbnms);
        }
        return list;
    }

    /**
     * 分页查询
     *
     * @param dto 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Question> queryAllLimit(QuestionDto dto) {
        List<Question> list = questionDao.queryAllBySize(dto);
        for (Question qt : list) {
            List<LabelInfo> labels = qt.getLabels();
            List<String> lbnms = labels.stream().map(LabelInfo::getLabelName).distinct()
                    .collect(Collectors.toList());
            qt.setQuestionLabels(lbnms);
        }
        return list;
    }

    @Override
    public Question queryByWrong(Integer questionId) {
        Question question = questionDao.queryById(questionId);
        List<LabelInfo> labels = question.getLabels();
        LabelInfo labelInfo = new LabelInfo();
        QuestionDto questionDto = new QuestionDto();
        List<Integer> labelIds = labels.stream().map(LabelInfo::getId).distinct()
                .collect(Collectors.toList());
        questionDto.setQuestionLabelIds(labelIds);
        List<Question> questions = questionDao.queryAll(questionDto);
        if (!CollectionUtils.isEmpty(questions)) {
            questions = questions.stream().filter(a -> a.getId() != questionId).collect(Collectors.toList());
        }
        if (!CollectionUtils.isEmpty(questions)) {
            return questions.get(0);
        } else {
            return null;
        }
    }

    /**
     * 新增数据
     *
     * @param question 实例对象
     * @return 实例对象
     */
    @Override
    public Question insert(Question question) {
        question.setCrtDt(new Date());
        question.setOptDt(new Date());
        questionDao.insert(question);
        List<QuestionLabel> qlbs = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(question.getQuestionLabelIds())) {
            List<Integer> questionLabels = question.getQuestionLabelIds();
            for (Integer qlb : questionLabels) {
                QuestionLabel ql = new QuestionLabel();
                ql.setQuestionId(question.getId());
                ql.setLabelId(qlb);
                qlbs.add(ql);
            }
            questionLabelDao.insertBatch(qlbs);
        }
        return question;
    }

    /**
     * 修改数据
     *
     * @param question 实例对象
     * @return 实例对象
     */
    @Override
    public Question update(Question question) {
        question.setOptDt(new Date());
        if (!CollectionUtils.isEmpty(question.getQuestionLabelIds())) {
            List<Integer> questionLabelIds = question.getQuestionLabelIds();
            questionLabelDao.deleteByQuestionId(question.getId());
            List<QuestionLabel> questionLabels = new ArrayList<>();
            for (Integer questionLabelId : questionLabelIds) {
                QuestionLabel questionLabel = new QuestionLabel();
                questionLabel.setQuestionId(question.getId());
                questionLabel.setLabelId(questionLabelId);
                questionLabels.add(questionLabel);
            }
            questionLabelDao.insertBatch(questionLabels);
        }
        questionDao.update(question);
        return queryById(question.getId());
    }

    /**
     * 批量修改数据
     *
     * @param list 实例对象
     * @return 实例对象
     */
    @Override
    public int updateBatch(List<Question> list) {
        list.forEach(a -> {
            a.setOptDt(new Date());
        });
        return questionDao.updateBatch(list);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return questionDao.deleteById(id) > 0;
    }
}
